package com.muyu.cloud.commodity.service.imp;

import com.muyu.cloud.commodity.domain.req.ProductStatsReq;
import com.muyu.cloud.commodity.domain.resp.ProductStatsResp;
import com.muyu.cloud.commodity.mapper.ProductStatsMapper;
import com.muyu.cloud.commodity.service.ProductStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * 商品统计服务实现
 * @author muyu
 * @date 2025/01/20
 */
@Service
public class ProductStatsServiceImpl implements ProductStatsService {

    @Autowired
    private ProductStatsMapper productStatsMapper;

    @Override
    public ProductStatsResp getStatsOverview(ProductStatsReq req) {
        // 设置默认日期范围（最近30天）
        if (req.getStartDate() == null) {
            req.setStartDate(LocalDate.now().minusDays(30));
        }
        if (req.getEndDate() == null) {
            req.setEndDate(LocalDate.now());
        }

        ProductStatsResp resp = new ProductStatsResp();

        // 获取当前期间数据
        Long viewCount = productStatsMapper.getViewCount(req);
        Long productCount = productStatsMapper.getProductCount(req);
        Long totalSales = productStatsMapper.getTotalSales(req);
        Double totalAmount = productStatsMapper.getTotalAmount(req);

        resp.setViewCount(viewCount);
        resp.setProductCount(productCount);
        resp.setTotalSales(totalSales);
        resp.setTotalAmount(BigDecimal.valueOf(totalAmount != null ? totalAmount : 0.0));

        // 计算环比增长
        ProductStatsReq prevReq = new ProductStatsReq();
        prevReq.setStartDate(req.getStartDate().minusDays(req.getEndDate().toEpochDay() - req.getStartDate().toEpochDay() + 1));
        prevReq.setEndDate(req.getStartDate().minusDays(1));
        prevReq.setProductId(req.getProductId());
        prevReq.setCategoryId(req.getCategoryId());
        prevReq.setBrandId(req.getBrandId());

        Long prevViewCount = productStatsMapper.getViewCount(prevReq);
        Long prevProductCount = productStatsMapper.getProductCount(prevReq);
        Long prevTotalSales = productStatsMapper.getTotalSales(prevReq);
        Double prevTotalAmount = productStatsMapper.getTotalAmount(prevReq);

        ProductStatsResp.GrowthData growthData = new ProductStatsResp.GrowthData();
        growthData.setViewCountGrowth(calculateGrowthRate(viewCount, prevViewCount));
        growthData.setProductCountGrowth(calculateGrowthRate(productCount, prevProductCount));
        growthData.setSalesGrowth(calculateGrowthRate(totalSales, prevTotalSales));
        growthData.setAmountGrowth(calculateGrowthRate(totalAmount, prevTotalAmount));

        resp.setGrowthData(growthData);

        // 获取趋势数据
        List<ProductStatsResp.TrendData> trendData = productStatsMapper.getTrendData(req);
        resp.setTrendData(trendData);

        // 获取商品排行
        List<ProductStatsResp.ProductRanking> productRanking = productStatsMapper.getProductRanking(req);
        resp.setProductRanking(productRanking);

        return resp;
    }

    @Override
    public ProductStatsResp getTrendData(ProductStatsReq req) {
        // 设置默认日期范围
        if (req.getStartDate() == null) {
            req.setStartDate(LocalDate.now().minusDays(30));
        }
        if (req.getEndDate() == null) {
            req.setEndDate(LocalDate.now());
        }

        ProductStatsResp resp = new ProductStatsResp();
        List<ProductStatsResp.TrendData> trendData = productStatsMapper.getTrendData(req);
        resp.setTrendData(trendData);

        return resp;
    }

    @Override
    public ProductStatsResp getProductRanking(ProductStatsReq req) {
        // 设置默认日期范围
        if (req.getStartDate() == null) {
            req.setStartDate(LocalDate.now().minusDays(30));
        }
        if (req.getEndDate() == null) {
            req.setEndDate(LocalDate.now());
        }

        ProductStatsResp resp = new ProductStatsResp();
        List<ProductStatsResp.ProductRanking> productRanking = productStatsMapper.getProductRanking(req);
        resp.setProductRanking(productRanking);

        return resp;
    }

    /**
     * 计算增长率
     */
    private Double calculateGrowthRate(Number current, Number previous) {
        if (previous == null || previous.doubleValue() == 0) {
            return current != null && current.doubleValue() > 0 ? 100.0 : 0.0;
        }
        if (current == null) {
            return -100.0;
        }
        return ((current.doubleValue() - previous.doubleValue()) / previous.doubleValue()) * 100;
    }
}



